package com.hlw.springboot.util;

import java.util.HashMap;
import java.util.Map;

public class ToLayuiTableUtil {
    private Integer page;
    private Integer limit;
    private Map<String, Object> data;

    private ToLayuiTableUtil() {

    }

    public static void toMysqlPage(Map<String, Object> map) {
        Integer page = (Integer)map.get("page");
        Integer limit = (Integer)map.get("limit");
        if(page != null && limit != null) {
            map.put("page", (page - 1) * limit);
        }
    }

    public static ToLayuiTableUtil setMap(Map<String, Object> map) {
        ToLayuiTableUtil toLayuiTable = new ToLayuiTableUtil();
        Integer page = (Integer)map.get("page");
        Integer limit = (Integer)map.get("limit");
        if(page != null && limit != null) {
            toLayuiTable.page = page;
            toLayuiTable.limit = limit;
        }
        toLayuiTable.data = new HashMap<String, Object>();
        if( map.get("data") != null) {
            toLayuiTable.data.putAll((Map<String, Object>) map.get("data"));
        }
        if(map.get("orderBy") != null && !("".equals(map.get("orderBy")))) {
            toLayuiTable.data.put("orderBy", "order by " + map.get("orderBy"));
        }

        return toLayuiTable;
    }

    public Integer getPage() {
        return page;
    }

    public Integer getLimit() {
        return limit;
    }

    public Map<String, Object> getData() {
        return data;
    }
}
